﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace InfoboardAdapterC.Common
{
    public class IniFileConfig
    {
        private readonly string _Path = @"\ConfigIAC.ini";
        //声明读写INI文件的API函数 ：DllImport 在 System.Runtime.InteropService 包里
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        //包含非托管代码的DLL名称
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        public IniFileConfig()
        {
            _Path = System.Windows.Forms.Application.StartupPath + _Path;

        }

        /// <summary>
        /// 写INI文件
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void IniWriteValue(string section, string key, string value)
        {
            WritePrivateProfileString(section, key, value, _Path);
        }
        /// <summary>
        /// 读取INI文件指定 
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public string IniReadValue(string section, string key)
        {
            var temp = new StringBuilder(255);
            GetPrivateProfileString(section, key, "", temp, 255, _Path);
            return temp.ToString();
        }
    }
}
